/***
This do-file creates 2 figures: (1) changes in small business revenue vs income 
share of top 1% of the income distribution and (2) changes in small business
revenue vs share of population below poverty line.
***/

*-------------------------------------------------------------------------------
* Set up 
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Small Business Revenue"

* Create required subfolders
cap mkdir "${root}/results/`category'"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

*-------------------------------------------------------------------------------
* Get the small business revenue data
*-------------------------------------------------------------------------------
project, uses("${root}/data/web/data/Womply - County - Weekly.csv")
import delimited "${root}/data/web/data/Womply - County - Weekly.csv", clear

* Only keep data across all industries
rename *_all *
rename countyfips county_fips

gen date = mdy(month, day_endofweek, year)
gen week = week(date)

*-------------------------------------------------------------------------------
* Generate the average decline in the post-COVID period (March 25-April 14)
*-------------------------------------------------------------------------------

keep if year == 2020 & inrange(week, 13, 15)

* Collapse by county across all dates
collapse (mean) revenue, by(county_fips)

compress

* Merge small business revenue data with county-level covariates
project, uses("${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta")
merge m:1 county_fips using "${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta", ///
keep(1 3) nogen keepusing(poorshare_2014_2018_est gini_2014_2018_est pop_2014_2018_est)

rename (pop_2014_2018_est gini_2014_2018_est poorshare_2014_2018_est county_fips) (pop_2018 gini_2018 poorshare_2018 countyfips)

project, uses("${root}/data/dvc/Opportunity Atlas/county_covars_atlas.dta")
merge m:1 countyfips using "${root}/data/dvc/Opportunity Atlas/county_covars_atlas.dta", keep(1 3) nogen keepusing(inc_share_1perc)

* Multiply proportions by 100 to obtain percentages
gen revenue_change_100 = revenue * 100
gen poor_share2018_100 = poorshare_2018 
gen inc_share_1perc_100 = inc_share_1perc * 100

* Winsorize change in revenue 
sum revenue_change_100 [w = pop_2018], d 
replace revenue_change_100 = `r(p99)' if revenue_change_100 >= `r(p99)' & !mi(revenue_change_100)

foreach var in poor_share2018_100 inc_share_1perc_100   { 

	* Run regression 
	reg revenue_change_100 `var' [w = pop_2018], r 
	local coef : di %4.2f _b[`var']
	local se : di %4.2f _se[`var']
	
	if "`var'" == "poor_share2018_100" {
		local reglab 9.2
		local outcome "Share of Population below Poverty Line"
		local key "womply_poor"
		local ylabel -55 "-55%" -50 "-50%" -45 "-45%"
		local xlabel 5 "5%" 10 "10%" 15 "15%" 20 "20%" 25 "25%"
		local x_title "Share of the Population Below the Poverty Line in 2014-2018 (%)"
		local ytext "-54.5"
	}
	
	if "`var'" == "inc_share_1perc_100" {
		local reglab 11
		local outcome "Income Share of Top 1 Percent of Income Distribution"
		local key "womp_top1"
		local ylabel -60 "-60%" -55 "-55%" -50 "-50%" -45 "-45%" -40 "-40%"
		local xlabel 5 "5%" 10 "10%" 15 "15%" 20 "20%" 25 "25%" 30 "30%" 35 "35%"
		local x_title "Top 1% Income Share (%)"
		local ytext "-59"
	}

	if "`var'" != "" local scale ""

	binscatter revenue_change_100 `var' ///
		[w = pop_2018] , ///
		xtitle("`x_title'") ///
		xlabel(`xlabel', format(%9.0gc)) ///
		ytitle("Change in Small Business Revenue (%)" "from January to April 2020") ///
		ylabel(`ylabel', nogrid) ///
		text(`ytext' `reglab' "Slope = `coef'% (s.e. = `se')", size(medlarge) color(gs8)) /// 
		${title}
	
	oi_graph_export "${root}/results/Small Business Revenue/Changes in Small Business Revenue vs `outcome'", type(${fig_type})

	*-------------------------------------------------------------------------------
	* Export output numbers to csv file
	*-------------------------------------------------------------------------------

	cap erase "${root}/results/paper numbers/`category'/Changes in Small Business Revenue vs. `outcome'.yaml"
	
	yamlout using "${root}/results/paper numbers/`category'/Changes in Small Business Revenue vs. `outcome'.yaml", ///
		key("`key'_slope") ///
		comment("Slope (%/$1000)") ///
		value(`coef') fmt(%9.2f)
	yamlout using "${root}/results/paper numbers/`category'/Changes in Small Business Revenue vs. `outcome'.yaml", ///
		key("`key'_se") ///
		comment("SE") ///
		value(`se') fmt(%9.2f)

	project, creates("${root}/results/paper numbers/`category'/Changes in Small Business Revenue vs. `outcome'.yaml")

}
